package com.share.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.time.LocalDateTime;

/**
 * Mybatis plus 配置类。
 *
 * @author liw
 * @version 1.0
 * @date 2022/8/28 15:20
 */
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig {

    private static String CREATE_TIME = "createTime";
    private static String UPDATE_TIME = "updateTime";
    private static String CREATE_USER = "createUser";
    private static String UPDATE_USER = "updateUser";


    /**
     * 启动乐观锁
     * 支持mysql物理分页
     *
     * @return MybatisPlusInterceptor
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //乐观锁拦截器
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        //支持mysql
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));

        return interceptor;
    }

    /**
     * 支持数据库创建时间和更新时间自动生成
     *
     * @return MetaObjectHandler
     */
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MetaObjectHandler() {

            @Override
            public void insertFill(MetaObject metaObject) {


                if (metaObject.hasSetter(CREATE_TIME) && metaObject.getValue(CREATE_TIME) == null) {
                    this.setFieldValByName(CREATE_TIME, LocalDateTime.now(), metaObject);
                }
                if (metaObject.hasSetter(UPDATE_TIME) && metaObject.getValue(UPDATE_TIME) == null) {
                    this.setFieldValByName(UPDATE_TIME, LocalDateTime.now(), metaObject);
                }

            }

            @Override
            public void updateFill(MetaObject metaObject) {

                if (metaObject.hasSetter(UPDATE_TIME) && metaObject.getValue(UPDATE_TIME) == null) {
                    this.setFieldValByName(UPDATE_TIME, LocalDateTime.now(), metaObject);
                }

            }
        };
    }


}
